package com.cloudlead.forwardAction.service.impl;
import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseFleetService;
import com.cloudlead.commonData.service.BaseShipRouteService;
import com.cloudlead.commonData.vo.BaseDock;
import com.cloudlead.commonData.vo.BaseFleet;
import com.cloudlead.commonData.vo.BaseShipRoute;
import com.cloudlead.forwardAction.service.ShipRouteSer;
import com.cloudlead.forwardService.vo.BaseShipRoute1Vo;
import com.cloudlead.forwardService.vo.BaseShipRouteVo;
import com.cloudlead.forwardService.vo.EaseUiVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/8/20.
 */
@Service
public class ShipRouteSerImpl implements ShipRouteSer {
    @Autowired
    BaseShipRouteService baseShipRouteService;
    @Autowired
    BaseFleetService baseFleetService;

    @Override
    //航线创建
    public void addShipRoute(BaseShipRoute1Vo baseShipRouteVo) {
        BaseShipRoute bsr=new BaseShipRoute();
        BeanUtils.copyProperties(baseShipRouteVo,bsr);
        bsr.setId(baseShipRouteVo.getId());
        bsr.setStartCity(baseShipRouteVo.getStartCity());
        bsr.setEndCity(baseShipRouteVo.getEndCity());
        bsr.setStatus(baseShipRouteVo.getStatus());
        bsr.setComments(baseShipRouteVo.getComments());
        bsr.setFlag("0");
        baseShipRouteService.create(bsr);
    }

    @Override
    //单值查询
    public BaseShipRouteVo findOneShipRoute(BaseShipRouteVo baseShipRouteVo) {
        BaseShipRoute bsp=new BaseShipRoute();
        BeanUtils.copyProperties(baseShipRouteVo,bsp);
        BaseShipRoute bsp1=baseShipRouteService.findOne(bsp);
        BaseShipRouteVo bspv=new BaseShipRouteVo();
        BeanUtils.copyProperties(bsp1, bspv);
        return bspv;
    }

    @Override
    //分页查询
    public Page<BaseShipRouteVo> findShipRoute(BaseShipRouteVo baseShipRouteVo) {
        Page<BaseShipRouteVo> baseShipRouteVo1 = new Page<BaseShipRouteVo>();
        PageRequest pageRequest = new PageRequest();
        List<Term> term = new ArrayList<Term>();
        term.add(new Term("flag", "0"));
        Filter filter = new Filter( new Criteria( term));
        pageRequest.setFilter(filter);
        pageRequest.setPage(1);
        pageRequest.setSize(3);
/*        List<BaseShipRoute> list =  baseShipRouteService.findAll(pageRequest).getContent();
        if(list.isEmpty()){
            return null;
        }else{
            for(BaseShipRoute br : list){
                BeanUtils.copyProperties(br,baseShipRouteVo1);
                list1.add(baseShipRouteVo1);
            }
        }*/
        Page<BaseShipRoute> page = baseShipRouteService.findAll(pageRequest);
        BeanUtils.copyProperties(page,baseShipRouteVo1);
        return  baseShipRouteVo1;
    }

    @Override
    //航线删除
    public void delShipRoute(String id) {
        BaseShipRoute bsp=baseShipRouteService.view(id);
        bsp.setFlag("1");
        baseShipRouteService.update(bsp);
    }

    public EaseUiVo testEasyUi(Integer pages,Integer row){
        Page<BaseShipRouteVo> baseShipRouteVo1 = new Page<BaseShipRouteVo>();
        PageRequest pageRequest = new PageRequest();
        List<Term> term = new ArrayList<Term>();
        term.add(new Term("flag", "0"));
        Filter filter = new Filter( new Criteria( term));
        pageRequest.setFilter(filter);
        pageRequest.setPage(pages);
        pageRequest.setSize(row);
        Page<BaseShipRoute> page = baseShipRouteService.findAll(pageRequest);
        EaseUiVo easeUiVo = new EaseUiVo();
        easeUiVo.setTotal(page.getTotalPages());
        easeUiVo.setFooter("");
        System.out.print(page.getContent().size()+"+++++++++++++++++++++++++++");
        List list = new ArrayList<>();
        for(int i=0;i<page.getContent().size();i++) {
            System.out.print("_________________________________");
            Map map = new HashMap<>();
            map.put("carName",page.getContent().get(i).getId());
            map.put("carSex", "女");

            list.add(map);
            System.out.print("sdfsdfdsf");
        }
        easeUiVo.setRows(list);
        return easeUiVo;
    }




   /* public EaseUiVo findAllFleetEasyUI(BaseFleetVo vo,Integer page,Integer rows) {
        BaseFleetVo bf1=new BaseFleetVo();
        if(vo!=null) bf1=vo;
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();

        tr.add(new Term("flag","0"));//未删除
        if(vo.getId()!=null&&(!vo.getId().equals("")))//id
            tr.add( new Term("id", vo.getId()));
        if(vo.getFleetName()!=null&&(!vo.getFleetName().equals("")))//车队名 模糊查询
            tr.add(new Term("fleetName",Operator.LK,"%"+vo.getFleetName()+"%"));
        if(vo.getAddress()!=null&&(!vo.getAddress().equals("")))//地址 模糊查询
            tr.add(new Term("address",Operator.LK,"%"+ vo.getAddress()+"%"));
        if(vo.getFleetSort()!=null&&(!vo.getFleetSort().equals(null)))
            tr.add(new Term("fleetSort",vo.getFleetSort()));//车队类别
        Filter filter=new Filter( new Criteria( tr));
        pr.setPage(page);
        pr.setSize(rows);
        pr.setFilter(filter);
        Page<BaseFleet> pb=(Page<BaseFleet>)baseFleetService.findAll(pr);
        Page<BaseFleetVo> pbf=new Page<BaseFleetVo>();
        BeanUtils.copyProperties(pb, pbf);

        EaseUiVo easeUiVo = new EaseUiVo();
        easeUiVo.setTotal(pbf.getTotalPages());
        easeUiVo.setFooter("");
        easeUiVo.setRows(pbf.getContent());
        return  easeUiVo;
    }*/
}
